using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace KeyAuthService.Dialogs
{
    /// <summary>
    /// 现代风格输入对话框
    /// </summary>
    public class ModernInputDialog : Window
    {
        private TextBox txtInput;
        
        public string InputText { get; private set; } = string.Empty;
        
        public ModernInputDialog(string prompt, string title)
        {
            Title = title;
            Style = (Style)FindResource("DialogWindowStyle");
            MinWidth = 400;
            MinHeight = 200;
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            Topmost = true;
            SizeToContent = SizeToContent.WidthAndHeight;
            ResizeMode = ResizeMode.NoResize;
            
            // 创建主容器
            var grid = new Grid();
            grid.Style = (Style)FindResource("DialogContainerStyle");
            
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
            
            // 提示文本
            var promptText = new TextBlock
            {
                Text = prompt,
                Style = (Style)FindResource("DialogPromptStyle")
            };
            
            // 输入框
            txtInput = new TextBox
            {
                Style = (Style)FindResource("DialogInputStyle")
            };
            txtInput.KeyDown += TxtInput_KeyDown;
            
            // 按钮面板
            var buttonPanel = new StackPanel
            {
                Style = (Style)FindResource("DialogButtonPanelStyle")
            };
            
            // 确定按钮
            var btnOK = new Button
            {
                Content = "确定",
                Style = (Style)FindResource("DialogPrimaryButtonStyle"),
                IsDefault = true
            };
            
            // 取消按钮
            var btnCancel = new Button
            {
                Content = "取消",
                Style = (Style)FindResource("DialogSecondaryButtonStyle"),
                IsCancel = true
            };
            
            btnOK.Click += (s, e) => AcceptInput();
            
            // 添加按钮到面板
            buttonPanel.Children.Add(btnCancel);
            buttonPanel.Children.Add(btnOK);
            
            // 添加控件到布局
            grid.Children.Add(promptText);
            Grid.SetRow(promptText, 0);
            
            grid.Children.Add(txtInput);
            Grid.SetRow(txtInput, 1);
            
            grid.Children.Add(buttonPanel);
            Grid.SetRow(buttonPanel, 2);
            
            Content = grid;
            
            // 加载后聚焦到输入框
            Loaded += (s, e) => 
            {
                txtInput.Focus();
            };
        }
        
        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                AcceptInput();
            }
        }
        
        private void AcceptInput()
        {
            InputText = txtInput.Text?.Trim() ?? string.Empty;
            DialogResult = true;
        }
    }
} 